package edu.chen.controller;

import edu.chen.dto.Result;
import edu.chen.entity.Lease;
import edu.chen.entity.User;
import edu.chen.service.LeaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("/lease")
public class LeaseController {
//    private Logger logger = Logger.getLogger(LeaseController.class);

    @Autowired
    private LeaseService leaseService;

    /**
     * 借阅请求处理方法,返回json数据
     * @param book_id
     * @param student_id
     * @return
     */
    @RequestMapping(value = "/{book_id}/leaseBook",method = RequestMethod.POST)
    @ResponseBody
    private Result leaseBook(@PathVariable int book_id, @RequestParam int student_id){
        if(student_id == 0){
            return new Result("学生id不能为空");
        }
        Lease lease = leaseService.leaseBook(book_id,student_id);
        return new Result<Lease>(lease);
//        BasicExecution basicExecution;
//        try{
//            basicExecution = leaseService.leaseBook(book_id,student_id);
//            return new Result<BasicExecution>(true,basicExecution);
//        }catch (NotFountException e){
//            basicExecution = new BasicExecution(LeaseStateEnum.NO_BOOK);
//        }catch (NoNumberException e){
//            basicExecution = new BasicExecution(LeaseStateEnum.NO_NUMBER);
//        }catch (RepeatLeaseException e){
//            basicExecution = new BasicExecution(LeaseStateEnum.REPEAT_LEASE);
//        }catch (LeaseException e){
//            basicExecution = new BasicExecution(LeaseStateEnum.INNER_ERROR);
//        }
//        return new Result<BasicExecution>(false,basicExecution);
    }

    /**
     * 跳转到用户的书本借阅信息页面
     * @param modelAndView
     * @param request
     * @return
     * @throws Exception
     */
    @GetMapping(value = "getLeaseByStudentId")
    private ModelAndView bookData(ModelAndView modelAndView, HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("loginUser");          //获取登陆的user信息
        modelAndView.setViewName("leaseData");                                               //指定跳转的页面名字
        int student_id = user.getStudent().getStudent_id();         //获取对应学生id
        if(student_id != 0) {
            List<Lease> leases = leaseService.getLeaseByStudentId(user.getStudent().getStudent_id());
            //因为租借信息中的学生信息在user信息中存在所以没有必要传到前段
            for (Lease lease : leases){
                lease.setStudent(null);
            }
            modelAndView.addObject("leaseList",leases);     //相当于request.setAttribute
//            modelAndView.getModel().put("bookList",commonExecution.getData());
        }
        return modelAndView;
    }


    /**
     * 归还书本的ajax所调用的的方法
     * @param book_id
     * @param student_id
     * @return
     */
    @PostMapping(value = "{book_id}/returnBook")
    @ResponseBody
    private Result returnBook(@PathVariable int book_id,@RequestParam int student_id){
        leaseService.returnBook(book_id,student_id);
        return new Result();
    }


}
